//
// Created by YxMin on 2024/7/12.
//
#include "../ui/element/Map.h"
#include "procedure.h"
#include "netdata.h"

bool IsPlayerInBase(Map &m,Player* pls,int id) {
    PixelXY xy = GetPlayerBaseXY(m,id);
    return xy.x-20 <=pls[id].x && xy.x+20 >= pls[id].x && xy.y-20 <= pls[id].y && xy.y+20 >= pls[id].y;
}
PixelXY GetPlayerBaseXY(Map &m,int id) {
    switch (id) {
        case 0:
            return m.getPixelXYByPosition(0,0);
        case 1:
            return m.getPixelXYByPosition(0,19);
        case 2:
            return m.getPixelXYByPosition(14,0);
        case 3:
            return m.getPixelXYByPosition(14,19);
    }
    return {-1,-1};
}

int GetShootX(Player &player) {
    switch (player.direction) {
        case 0:
            return player.x + 40;
        case 1:
            return player.x + 20;
        case 2:
            return player.x;
        case 3:
            return player.x + 20;
    }
    return 0;
}
int GetShootY(Player &player) {
    switch (player.direction) {
        case 0:
            return player.y + 20;
        case 1:
            return player.y;
        case 2:
            return player.y + 20;
        case 3:
            return player.y + 40;
    }
    return 0;
}

int GetMoveX(int direction) {
    switch (direction) {
        case 0:
            return 1;
        case 1:
        case 3:
            return 0;
        case 2:
            return -1;
    }
    return 0;
}

int GetMoveY(int direction) {
    switch (direction) {
        case 3:
            return 1;
        case 0:
        case 2:
            return 0;
        case 1:
            return -1;
    }
    return 0;
}

int GetLocalPlayerIdByXY(Player *pls, int x, int y) {
    for (int i = 0; i < 4; ++i) {
        if (pls[i].id == -1)
            continue;
        if (pls[i].x <= x && pls[i].x+40 >= x && pls[i].y <= y && pls[i].y+40 >= y) {
            return pls[i].id;
        }
    }
    return -1;
}
